package server

import (
	"blog_go/internal/handler"
	"blog_go/internal/middleware"
	"blog_go/pkg/helper/jwt"
	"blog_go/pkg/log"

	"github.com/gin-gonic/gin"
)

func NewServerHTTP(
	logger *log.Logger,
	jwt *jwt.JWT,
	userHandler handler.UserHandler,
	articleCategoryHandler handler.ArticleCategoryHandler,
	tagHandler handler.TagHandler,
	collectionHandler handler.CollectionHandler,
	articleCommentHandler handler.ArticleCommentHandler,
	articleHandler handler.ArticleHandler,
) *gin.Engine {
	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.Use(
		middleware.CORSMiddleware(),
	)

	//注册
	r.POST("/registerUser", userHandler.RegisterUser)

	//刷新access token有效期
	r.POST("/refresh", userHandler.Refresh)

	//登录
	r.POST("/loginUser", userHandler.LoginUser)
	r.POST("/loginAdmin", userHandler.LoginAdmin)

	//查询标签、分类
	r.GET("/queryTag", tagHandler.QueryTag)
	r.GET("/queryCategory", articleCategoryHandler.QueryArticleCategory)

	//查询文章
	r.GET("/queryAllArticle", articleHandler.QueryAllArticle)
	r.GET("/queryArticleByArticleId", articleHandler.QueryArticleByArticleId)
	r.GET("/queryArticles", articleHandler.QueryArticles)
	r.GET("/queryUserArticle", articleHandler.QueryUserArticle)

	//查询评论
	r.GET("/queryComment", articleCommentHandler.QueryComment)

	//浏览文章
	r.POST("/viewArticle", articleHandler.ViewArticle)

	//查询他人信息
	r.GET("/queryUser", userHandler.QueryUser)

	//用户
	user := r.Group("/user", middleware.JwtAuthMiddleware(jwt), middleware.AuthStatusMiddleware())
	{
		//关注
		user.GET("/queryFollowUser", userHandler.QueryFollowUser)
		user.POST("/followUser", userHandler.FollowUser)

		//文章
		user.GET("/queryCollectedArticle", articleHandler.QueryCollectedArticle)
		user.POST("/addArticle", articleHandler.AddArticle)
		user.POST("/addMdArticle", articleHandler.AddMdArticle)
		user.POST("/updateMdArticle", articleHandler.UpdateArticle)
		user.POST("/updateArticle", articleHandler.UpdateArticle)

		user.POST("/collectArticle", articleHandler.CollectArticle)
		user.POST("/likeArticle", articleHandler.LikeArticle)
		user.POST("/disLikeArticle", articleHandler.DisLikeArticle)
		user.POST("/updateUploadFile", articleHandler.UpdateUploadFile)

		//文章评论
		user.POST("/addComment", articleCommentHandler.AddComment)
		user.POST("/likeArticleComment", articleCommentHandler.LikeArticleComment)
		user.POST("/disLikeArticleComment", articleCommentHandler.DisLikeArticleComment)

		//收藏夹
		user.GET("/queryCollection", collectionHandler.QueryCollection)
		user.POST("/addCollection", collectionHandler.AddCollection)
		user.POST("/updateCollection", collectionHandler.UpdateCollection)
		user.POST("/delCollection", collectionHandler.DelCollection)

		//修改个人资料
		user.POST("/updateUserProfile", userHandler.UpdateUserProfile)
	}

	admin := r.Group("/admin", middleware.JwtAuthMiddleware(jwt)) //管理员
	{
		//分类
		admin.POST("/addCategory", articleCategoryHandler.AddArticleCategory)
		admin.POST("/updateCategory", articleCategoryHandler.UpdateArticleCategory)
		admin.POST("/delCategory", articleCategoryHandler.DelArticleCategory)

		//标签
		admin.GET("/queryAllTag", tagHandler.QueryAllTag)
		admin.POST("/addTag", tagHandler.AddTag)
		admin.POST("/updateTag", tagHandler.UpdateTag)
		admin.POST("/delTag", tagHandler.DelTag)

		//用户
		admin.GET("/queryAllUser", userHandler.QueryAllUser)
		admin.POST("/updateUserStatus", userHandler.UpdateUserStatus)
		admin.POST("/resetPwd", userHandler.ResetUserPwd)
		admin.POST("/delUser", userHandler.DelUser)

		//审核、评论
		admin.POST("/reviewArticle", articleHandler.ReviewArticle)
		admin.POST("/reviewComment", articleCommentHandler.ReviewComment)
		admin.POST("/reviewAllArticle", articleHandler.ReviewAllArticle)
		admin.POST("/reviewAllComment", articleCommentHandler.ReviewAllComment)
	}

	//用户和管理员
	currency := r.Group("/currency", middleware.JwtAuthMiddleware(jwt), middleware.AuthStatusMiddleware())
	{
		currency.POST("/updatePwd", userHandler.UpdatePwd)
		currency.GET("/queryArticle", articleHandler.QueryArticle)
		currency.POST("/delArticle", articleHandler.DelArticle)
		currency.POST("/delComment", articleCommentHandler.DelComment)
		currency.GET("/queryAllComment", articleCommentHandler.QueryAllComment)
	}

	//资源
	r.Static("/resources/", "resources/")

	return r
}
